(function(){
    angular.module('app').filter('toTrusted', ['$sce', function($sce) {
        return function(text) {
            return $sce.trustAsHtml(text);
        };
    }]).controller("agbIndexCtrl",function($scope,proxy){
        $scope.targetList = [
            {
                title: '竞价交易当日花费(元)',
                t:'rtbSpend',
                yt:'rtbYSpend',
                ytb:'rtbYbSpend',
            },{
                title: '竞价交易当日利润(元)',
                t:'rtbProfit',
                yt:'rtbYProfit',
                ytb:'rtbYbProfit',
            },{
                title: '竞价交易当日利润率',
                t:'rtbProfitRate',
                yt:'rtbYProfitRate',
                ytb:'rtbYbProfitRate',
            },{
                title: '广告主数量',
                t:'customerNum'
            },{
                title: '活跃广告主数量',
                t:'customerActiveNum'
            },{
                title: '待审核广告主',
                t:'customerApplyNum'
            },{
                title: '合约交易(保价不保量)当日花费(元)',
                t:'pdSpend',
                yt:'pdYSpend',
                ytb:'pdYbSpend',
            },{
                title: '合约交易(保价不保量)当日利润(元)',
                t:'pdProfit',
                yt:'pdYProfit',
                ytb:'pdYbProfit',
            },{
                title: '合约交易(保价不保量)当日利润率',
                t:'pdProfitRate',
                yt:'pdYProfitRate',
                ytb:'pdYbProfitRate',
            },{
                title: '合约交易(保价保量)当日收入(元)',
                t:'pdbIncome',
                yt:'pdbYIncome',
                ytb:'pdbYbIncome',
            }
        ];

        $scope.permission = PERMISSION;
        // 同时使用$watch和$on获取permission值的原因：
        // permission与此js都通过ajax请求返回
        // 当permission请求快于此页面时会导致父级$broadcast广播时子级还未注册接收事件导致错过广播
        // 所以用$watch监听PERMISSION变量是否变化
        $scope.$watch('permission',function (newValue, oldValue) {
            if(angular.equals({},newValue)) return;
            setTatgetList(newValue)
        });
        // 当此js请求快于permission请求时，PERMISSION为{},此时当permission请求返回后无法触发$watch
        // 所以用$on来监听广播
        $scope.$on('permission',function (e, permission) {
            setTatgetList(permission)
        });

        $scope.pd = {}

        $scope.advDayPage = {
            page: 1,
            total: 0
        }
        $scope.advCountPage = {
            page: 1,
            total: 0
        }
        $scope.dayList = [];
        $scope.countList = [];
        $scope.getAdvDay = getAdvDay;
        $scope.getAdvCount = getAdvCount;
        $scope.eTime = getNowFormatDate();
        $scope.sTime = nextDayFnc($scope.eTime,30,1);
        getTarget();
        getAdvDay();
        getAdvCount();
        getAdvDayTotal();
        getAdvCountTotal();

        // 代理商首页指标
        function getTarget() {

            var data = {
                'filter': {},
                "sort": ["cTime,1"],
                sTime : $scope.eTime+" 00:00:00",
                eTime : $scope.eTime+" 23:59:59"
            };
            // 竞价交易总额
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/agency/main',
                data: data,
                success: function(res){
                    var target = res.data.data;
                    target.rtbProfitRate = (target.rtbProfitRate*100).toFixed(2)+"%";
                    target.rtbYProfitRate = (target.rtbYProfitRate*100).toFixed(2)+"%";
                    target.rtbYbProfitRate = (target.rtbYbProfitRate*100).toFixed(2)+"%";
                    target.pdProfitRate = (target.pdProfitRate*100).toFixed(2)+"%";
                    target.pdYProfitRate = (target.pdYProfitRate*100).toFixed(2)+"%";
                    target.pdYbProfitRate = (target.pdYbProfitRate*100).toFixed(2)+"%";
                    $scope.target = target;
                }
            })
        }

        // 日花费
        function getAdvDay() {
            var data = {
                'page': $scope.advDayPage.page + "",
                'size': "10",
                'filter': {},
                "sort": ["cTime,1"],
                sTime : $scope.sTime+" 00:00:00",
                eTime : $scope.eTime+" 23:59:59"
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/consume',
                data: data,
                load:true,
                success: function(res){
                    $scope.dayList = $scope.dayList.concat(res.data.data);
                    $scope.advDayPage.total = res.data.total;
                    $scope.advDayPage.page++;
                }
            })
        }
        // 日花费总计
        function getAdvDayTotal() {
            var data = {
                sTime : $scope.sTime+" 00:00:00",
                eTime : $scope.eTime+" 23:59:59"
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/consume/total',
                data: data,
                success: function(res){
                    $scope.dayTotal = res.data.data;
                }
            })
        }
        // 30日花费
        function getAdvCount() {
            if($scope.countAllList) {
                $scope.countList = $scope.countList.concat($scope.countAllList.splice(0,10));
                return;
            }
            var data = {
                'page': $scope.advCountPage.page + "",
                'size': "99",
                'filter': {},
                "sort": ["cTime,1"],
                sTime : $scope.sTime+" 00:00:00",
                eTime : $scope.eTime+" 23:59:59"
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/dsp/timeChart',
                data: data,
                load: true,
                success: function(res){
                    $scope.countAllList = res.data.data.reverse();
                    $scope.advCountPage.total = $scope.countAllList.length;
                    $scope.countList = $scope.countList.concat($scope.countAllList.splice(0,10));
                }
            })
        }
        // 30日花费总计
        function getAdvCountTotal() {
            if($scope.countAllList) {
                $scope.countList = $scope.countList.concat($scope.countAllList.splice(0,10));
                return;
            }
            var data = {
                sTime : $scope.sTime+" 00:00:00",
                eTime : $scope.eTime+" 23:59:59"
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/rpt/dsp/timeChart/sum',
                data: data,
                success: function(res){
                    $scope.countTotal = res.data.data;
                }
            })
        }
        // 根据权限变更指标
        function setTatgetList(permission) {
            var targetList = $scope.targetList;
            if(!permission.view['首页'].sub || !permission.view['首页'].sub['客户信息']) {
                // 移除‘客户数量’ ‘活跃客户数量’ ‘待审核客户’
                for(var i = 0; i < targetList.length; i++) {
                    if(targetList[i].t == 'customerNum') {
                        targetList.splice(i,3);
                        break;
                    }
                }
            }
            if(!permission.view['首页'].sub || !permission.view['首页'].sub['成本收入']) {
                // 移除 利润相关字段
                for(var i = 0; i < targetList.length; i++) {
                    if(targetList[i].t == 'rtbProfit') {
                        targetList.splice(i,2);
                        i--;
                    }
                    if(targetList[i].t == 'pdProfit') {
                        targetList.splice(i,3);
                        break;
                    }
                }
            }
            // 指标一行为6个  不足6的倍数时  补充空对象
            while(targetList.length % 6 != 0) {
                targetList.push({});
            }
            $scope.permission = permission;
        }
    })
})();